/*
 * @Author: 橘子
 * @Date: 2022-09-30 21:54:25
 * @Project_description: 项目介绍
 * @Description: 代码是我抄的，不会也是真的
 */
import Cookies from 'js-cookie'

import setting from '@/setting'
import dayjs from 'dayjs'

const version = setting.appInfo.version
const baseName = setting.appInfo.name
const cookies = {}

/**
 * @description 存储 cookie 值
 * @param {String} name cookie name
 * @param {String} value cookie value
 * @param {Object} setting cookie setting
 */
cookies.set = function(name = 'default', value = '', cookieSetting = {}) {
  const currentCookieSetting = {
    expires: 1,
  }
  Object.assign(currentCookieSetting, cookieSetting)
  Cookies.set(`${baseName}-${version}-${name}`, value, currentCookieSetting)
}
/**
 * @description 存储 路由值
 * @param {String} name cookie name
 * @param {String} value cookie value
 * @param {Object} setting cookie 持续时间默认为配置的时间
 */
cookies.setRouter = function(name = 'default', value = '', cookieSetting = {}) {
  // 持续时间
  let time = dayjs().add(setting.axiosConfig.submitTile, 'millisecond').format()
  const currentCookieSetting = {
    expires: new Date(time),
  }
  Object.assign(currentCookieSetting, cookieSetting)
  Cookies.set(`${baseName}-${version}-${name}`, value, currentCookieSetting)
}
/**
 * @description 拿到 cookie 值
 * @param {String} name cookie name
 */
cookies.get = function(name = 'default') {
  return Cookies.get(`${baseName}-${version}-${name}`)
}

/**
 * @description 拿到 cookie 全部的值
 */
cookies.getAll = function() {
  return Cookies.get()
}

/**
 * @description 删除 cookie
 * @param {String} name cookie name
 */
cookies.remove = function(name = 'default') {
  return Cookies.remove(`${baseName}-${version}-${name}`)
}

//list转树
const listzhuantree=(list,pid)=>{
  let tree=[]
  list.forEach(item=>{
    if(item.pid==pid){
      item.children=listzhuantree(list,item.id)
      tree.push(item)
    }
  })
  return tree
}

export default cookies
